use serde::{Serialize, Deserialize};

use crate::util::log_helper::push_error;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all="camelCase")]
pub struct VersionInfo {
  pub hash: String,
  pub update_time: String,
}

impl VersionInfo {
  pub fn is_same_hash(&self, hash: &str) -> bool {
    self.hash == hash
  }

  pub fn is_eq(&self, other: &Self) -> bool {
    self.hash == other.hash
  }

  pub fn from_json_str(json_str: &str) -> Option<VersionInfo> {
    let json_result = serde_json::from_str::<VersionInfo>(json_str);
    if json_result.is_ok() {
      let version_info = json_result.unwrap();
      return Some(version_info);
    }
    push_error(format!("parse version info error: {};\nraw: {}", json_result.err().unwrap(), json_str));
    return None;
  }
}
